#include "mobilestorageviewwidget.h"

#include <QScrollBar>
#include <QTableView>
#include <QObject>
#include <QDebug>
#include <QFileInfo>

const int VIEW_HEIGHT = 375;

MobileStorageViewWidget::MobileStorageViewWidget(const QMap<QString, FileInfo> &listStruct, QWidget *parent)
    : QWidget(parent)
{
    m_listStruct = listStruct;


    if (listStruct.size() == 0) {
        QVBoxLayout *vLayout = new QVBoxLayout();
        this->setLayout(vLayout);

        QWidget *hWidget = new QWidget(this);
        vLayout->addStretch();
        vLayout->addWidget(hWidget);
        vLayout->addStretch();

        QHBoxLayout *hLayout = new QHBoxLayout();
        hWidget->setLayout(hLayout);
        QLabel *label = new QLabel(this);
        label->setText(tr("No file")); // No file无文件
        hLayout->addStretch();
        hLayout->addWidget(label);
        hLayout->addStretch();
    } else {
        initViewWidget();
    }
    this->setAcceptDrops(true);
}

void MobileStorageViewWidget::initViewWidget()
{
    QVBoxLayout *mainVLayout = new QVBoxLayout();
    this->setLayout(mainVLayout);

    MobileStorageListView *listView = new MobileStorageListView();
    mainVLayout->addWidget(listView);
    mainVLayout->addStretch();
    mainVLayout->setContentsMargins(0, 0, 0, 0);

    //设置列表模式
    connect(this, &MobileStorageViewWidget::signalViewModeFlag, listView, [=](QListView::ViewMode mode) {
        listView->setViewModeFlag(mode);

        if (listView->viewMode() == QListView::IconMode) {
            mainVLayout->setContentsMargins(0, 18, 0, 0);
        } else {
            mainVLayout->setContentsMargins(20, 0, 0, 0);
        }

        listView->setResize();
    });

    //设置view的高度
    connect(listView, &MobileStorageListView::signalSetResize, this, [=](int height) {
        listView->setFixedHeight(height);
        //设置QListView所在界面的高度，防止当view的高度过小，对上传有影响
        if (height >= 0 && height <= VIEW_HEIGHT) {
            listView->setFixedHeight(VIEW_HEIGHT);
        }
    });

    //设置当前文件
    listView->setFileList(m_listStruct);
    listView->setResize();

    connect(listView, &MobileStorageListView::sigFileName, this, &MobileStorageViewWidget::slotFileName);

    connect(listView, &MobileStorageListView::sigShowCopyButton, this, &MobileStorageViewWidget::slotShowCopyButton);

    connect(listView, &MobileStorageListView::sigHideCopyToComputerWidget, this,
            &MobileStorageViewWidget::slotHideCopyToComputerWidget);

    connect(listView, &MobileStorageListView::sigSelectDataList, this, &MobileStorageViewWidget::slotSelectDataList);

    connect(listView, &MobileStorageListView::sigMousePress, this, [=](QMouseEvent *event) {
        Q_UNUSED(event);
        Q_EMIT sigHideCopyToComputerWidget();
        qDebug() << "MobileStorageViewWidget sigMousePress";
    });

    connect(listView, &MobileStorageListView::sigMouseRelease, this, [=]() {
        QList<FileInfo> list;
        listView->selectAllNames(list);
        if (list.length() > 0) {
            Q_EMIT sigSelectDataList(list);
            Q_EMIT sigShowCopyButton();
            QStringList selectListName;
            for (int i = 0; i < list.size(); i++) {
                selectListName << list.at(i).name();
            }
            qDebug() << "selectListName" << selectListName;
            qDebug() << "MobileStorageViewWidget sigMouseRelease";
        }
    });

    connect(listView, &MobileStorageListView::sigSelectAll, this, [=]() {
        listView->selectAll();
        Q_EMIT sigShowCopyButton();
    });

    connect(listView, &MobileStorageListView::sigMenuFileName, this, [=](QString fileName) {
        Q_EMIT sigMenuFileName(fileName);
    });

    connect(this, &MobileStorageViewWidget::sigViewHeight, listView, [=]() {
        listView->setViewHeight();
    });
}

void MobileStorageViewWidget::setMode(QListView::ViewMode mode)
{
    Q_EMIT signalViewModeFlag(mode);
}

void MobileStorageViewWidget::setViewHeight()
{
    Q_EMIT sigViewHeight();
}

void MobileStorageViewWidget::slotFileName(QString fileNamePath)
{
    Q_EMIT sigFileName(fileNamePath);
}

void MobileStorageViewWidget::slotShowCopyButton()
{
    Q_EMIT sigShowCopyButton();
}

void MobileStorageViewWidget::slotHideCopyToComputerWidget()
{
    Q_EMIT sigHideCopyToComputerWidget();
}

void MobileStorageViewWidget::slotSelectDataList(const QList<FileInfo> &fileList)
{
    Q_EMIT sigSelectDataList(fileList);
}

void MobileStorageViewWidget::dragEnterEvent(QDragEnterEvent *event)
{
    if (event->mimeData()->hasFormat("text/uri-list")) {
        event->acceptProposedAction();
    } else {
        event->ignore();
    }
    QWidget::dragEnterEvent(event);
}

void MobileStorageViewWidget::dropEvent(QDropEvent *event)
{
    auto urls = event->mimeData()->urls();
    qInfo() << "drag urls" << urls;
    if (urls.isEmpty()) {
        return;
    }

    QStringList localpath;
    for (auto &url : urls) {
        localpath << url.toLocalFile();
    }

    if (!localpath.isEmpty()) {
        qInfo() << "localpath" << localpath;
        Q_EMIT sigLocalPathList(localpath);
    }
}
